<%@page import="entity.Category"%>
<%@page import="service.CategoryServiceLocal"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="entity.Product"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="service.ProductServiceLocal"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<!--  start nav-outer-repeat................................................................................................. START -->
<div class="nav-outer-repeat"> 
<!--  start nav-outer -->
<div class="nav-outer"> 

		<!-- start nav-right -->
		<div id="nav-right">
                        <div class="showhide-account">&nbsp;</div>
			<div class="nav-divider">&nbsp;</div>
			<a href="<c:url value="/admin/login.jsp" />" id="logout"><img src="<c:url value="/layout/default/shared/nav/nav_logout.gif" />" width="64" height="14" alt="" /></a>
			<div class="clear">&nbsp;</div>
		
		</div>
		<!-- end nav-right -->


		<!--  start nav -->
		<div class="nav">
		<div class="table">
		
		                    
		<ul class="current"><li><a href="<c:url value="/admin/view_product.jsp" />"><b>Sản Phẩm</b><!--[if IE 7]><!--></a><!--<![endif]-->
		<!--[if lte IE 6]><table><tr><td><![endif]-->
		<div class="select_sub show">
			<ul class="sub">
				<li class="sub_show"><a href="<c:url value="/admin/view_product.jsp" />">Xem Sản Phẩm</a></li>
				<li><a href="<c:url value="/admin/update_product.jsp" />">Cập Nhật Sản Phẩm</a></li>
				<li><a href="<c:url value="/admin/add_product.jsp" />">Thêm Sản Phẩm</a></li>
			</ul>
		</div>
		<!--[if lte IE 6]></td></tr></table></a><![endif]-->
		</li>
		</ul>
		
		<div class="nav-divider">&nbsp;</div>
		
		<ul class="select"><li><a href="<c:url value="/admin/add_category.jsp" />"><b>Loại Sản Phẩm</b><!--[if IE 7]><!--></a><!--<![endif]-->
		<!--[if lte IE 6]><table><tr><td><![endif]-->
		<div class="select_sub">
			<ul class="sub">
				<li><a href="<c:url value="/admin/add_category.jsp" />">Thêm Loại Sản Phẩm</a></li>
			</ul>
		</div>
		<!--[if lte IE 6]></td></tr></table></a><![endif]-->
		</li>
		</ul>
		
		<div class="nav-divider">&nbsp;</div>
		
		<ul class="select"><li><a href="<c:url value="/admin/update_order.jsp" />"><b>Đơn Hàng</b><!--[if IE 7]><!--></a><!--<![endif]-->
		<!--[if lte IE 6]><table><tr><td><![endif]-->
		<div class="select_sub">
			<ul class="sub">
				<li><a href="<c:url value="/admin/update_order.jsp" />">Cập Nhật Trạng Thái Đơn Hàng</a></li>
			 
			</ul>
		</div>
		<!--[if lte IE 6]></td></tr></table></a><![endif]-->
		</li>
		</ul>		
		
		<div class="clear"></div>
		</div>
		<div class="clear"></div>
		</div>
		<!--  start nav -->

</div>
<div class="clear"></div>
<!--  start nav-outer -->
</div>
<!--  start nav-outer-repeat................................................... END -->

 <div class="clear"></div>
 
<!-- start content-outer ........................................................................................................................START -->
<div id="content-outer">
<!-- start content -->
<div id="content">

	<!--  start page-heading -->
	<div id="page-heading">
		<h1>Xem Sản Phẩm</h1>
	</div>
	<!-- end page-heading -->
	<table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
	<tr>
		<th rowspan="3" class="sized"><img src="<c:url value="/layout/default/shared/side_shadowleft.jpg" />" width="20" height="300" alt="" /></th>
		<th class="topleft"></th>
		<td id="tbl-border-top">&nbsp;</td>
		<th class="topright"></th>
		<th rowspan="3" class="sized"><img src="<c:url value="/layout/default/shared/side_shadowright.jpg" />" width="20" height="300" alt="" /></th>
	</tr>
	<tr>
		<td id="tbl-border-left"></td>
		<td>
		<!--  start content-table-inner ...................................................................... START -->
		<div id="content-table-inner">
                    
			<!--  start table-content  -->
			<div id="table-content">
<% 
    String pid_str = "Mã Sản Phẩm";
    if(request.getAttribute("pid_str") != null) {
        pid_str = request.getAttribute("pid_str").toString();
    }
    String pname_str = "Tên Sản Phẩm";
    if(request.getAttribute("pname_str") != null) {
        pname_str = request.getAttribute("pname_str").toString();
    }
    String cid_str = "0";
    if(request.getAttribute("cid_str") != null) {
        cid_str = request.getAttribute("cid_str").toString();
    }    
%>                            
                            <table border="0"><tr>
                                    <td><form class="search-wrapper" method="post" action="<c:url value="/admin/search_product.do" />">
			<input type="text" class="search-field" value="<%=pid_str%>" name="pid" />
			<input type="submit" class="search-button dark" value="Tìm Kiếm" />
                    </form></td>
                    <td rowspan="2" align="center" valign="center" style="padding-left: 30px;">  
                        <form method="post" action="<c:url value="/admin/search_product.do" />">
                                   <fieldset style="border: 1px solid lightgrey;float:left;
	width: 250px;margin-bottom: 30px;">
<legend style="font-size:13px;
	font-weight:bold;
	padding-bottom: 10px;">Loại Sản Phẩm</legend>
                                       <table border="0" style="margin-left: 10px; margin-bottom: 10px;"><tr><td><select name="cid" class="styledselect_pages">
				<option value="0">-- chọn --</option>
            <% 
                CategoryServiceLocal categoryServiceLocal = (CategoryServiceLocal) application.getAttribute("categoryServiceLocal");
                List<Category> lstCategories = categoryServiceLocal.getAllCategories();
                for(int i = 0; i < lstCategories.size(); i++) {
                    String cid = lstCategories.get(i).getCategoryID();
                    String cname = lstCategories.get(i).getCategoryName();               
                    if(cid.equals(cid_str)) {  %>                     
                    <option value="<%=cid%>" selected="true"><%=cname%></option>
               <% } else { %>                   
                    <option value="<%=cid%>"><%=cname%></option>
               <% } } %>
                            </select></td>
                            <td><input type="submit" class="button lightText small regular black" value='Tìm Kiếm' /></td>
                                           </tr></table></fieldset>
                    </form></td> </tr>                           
                                <tr><td><form class="search-wrapper" method="post" action="<c:url value="/admin/search_product.do" />">
                                <input type="text" class="search-field" value="<%=pname_str%>" name="pname" />
			<input type="submit" class="search-button dark" value="Tìm Kiếm" />
                                        </form></td></tr>
</table>
                            <form id="mainform" action="">
<% 
  ProductServiceLocal productServiceLocal = (ProductServiceLocal) application.getAttribute("productServiceLocal");
    List lstProducts = productServiceLocal.getAllProducts();
    if(request.getAttribute("products_search") != null) {
        lstProducts = (List<Product>) request.getAttribute("products_search");
    }
    if(lstProducts.size() > 0) {
%>                                
				<table border="1" style="border: 1px solid #d2d2d2;" width="100%" cellpadding="0" cellspacing="0" id="product-table">
				<tr bgcolor="#515151" style="color: #fff; padding-left: 0px; padding-right: 0px;
	font-family: Tahoma;
	font-size: 13px;
	font-weight: bold;
	line-height: 14px;">
					<th style="text-align: center;">#</th>
					<th style="text-align: center;">Hình</th>
					<th style="text-align: center;">Mã Sản Phẩm</th>
					<th style="text-align: center;">Tên Sản Phẩm</th>
					<th style="text-align: center;">SL Tồn Kho</th>
                                        <th style="text-align: center;">Đơn Giá (đ)</th>
                                        <th style="text-align: center;">Loại Sản Phẩm</th>
					<th style="text-align: center;">Lựa Chọn</th>
				</tr>
<%
    Iterator products = lstProducts.iterator();
    int j = 0;
    while(products.hasNext()) {
        Product product = (Product) products.next();
        String color = ++j % 2 == 0 ? "alternate-row" : "";
        String pid = product.getProductID();
        String pname = product.getProductName();
        String imagename = product.getImageName();
        BigDecimal unitpricefmt = product.getUnitPrice();
        int qoh = product.getQoh();
        String CName = product.getCategoryID().getCategoryName();
%>                                
				<tr class="<%=color%>" style="padding-left: 0px; padding-right: 0px;">
					<td style="text-align: center;"><%=j%></td>
					<td style="text-align: center;"><img width=50 title="<%=pname%>" height=50 src="../images/products/<%=imagename%>" /></td>
					<td style="text-align: center;"><%=pid%></td>
					<td style="text-align: center;"><%=pname%></td>
					<td style="text-align: center;"><%=qoh%></td>
                                        <td style="text-align: center;"><fmt:formatNumber type="currency" currencySymbol="" maxFractionDigits="0" value="<%=unitpricefmt%>"/></td>
                                        <td style="text-align: center;"><%=CName%></td>
					<td style="text-align: center;">
					<a href="../admin/update_product.jsp?<%=pid%>" style="color: blue;">Sửa</a>&nbsp;&nbsp;&nbsp;&nbsp;
					<a href="../admin/delete_product.do?<%=pid%>" style="color: blue;">Xóa</a>
					</td>
				</tr>
<% } %>
				</table> <!--  end product-table................................... --> 
<% } else { %>                                
<p style="font-weight:bold;">Không tìm thấy bất kỳ sản phẩm nào!</p>		
<% } %>
				</form> 
                        
			</div>
			<!--  end content-table  -->				
			
			<div class="clear"></div>
		 
		</div>
		<!--  end content-table-inner ............................................END  -->
		</td>
		<td id="tbl-border-right"></td>
	</tr>
	<tr>
		<th class="sized bottomleft"></th>
		<td id="tbl-border-bottom">&nbsp;</td>
		<th class="sized bottomright"></th>
	</tr>
	</table>
	<div class="clear">&nbsp;</div>

</div>
<!--  end content -->
<div class="clear">&nbsp;</div>
</div>
<!--  end content-outer........................................................END -->
<% 
if(((HttpServletRequest) request).getAttribute("notify") != null) {
    String notify = ((HttpServletRequest) request).getAttribute("notify").toString();
    if(notify.length() > 0) { 
        out.println("<script type=\"text/javascript\">");
        out.println("alert(\"" + notify + "\")");
        out.println("</script>");
    }
} %>